package com.parktech.sun.web.controller.management.aboutus;

import com.parktech.sun.domain.model.aboutus.AboutUs;
import com.parktech.sun.domain.service.aboutus.AboutUsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.Map;

/**
 * User: Think
 * Date: 11-1-13
 * Time: 下午10:58
 */
@Controller
@RequestMapping("/management/aboutus")
public class AboutUsController {

    private AboutUsService aboutUsService;

    @Autowired
    public AboutUsController(AboutUsService aboutUsService) {
        this.aboutUsService = aboutUsService;
    }
    @ModelAttribute("allAboutUs")
    public Map<String, String> populateAllAboutUs() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("introduction", "协会简介");
        map.put("framework", "组织架构");
        map.put("constitution", "协会章程");
        map.put("memorabilia", "大记事");
        map.put("cooperation", "合作伙伴");
        map.put("unionBrief", "协会简报");
        map.put("contact", "联系我们");
        map.put("enterpriseIntroduction", "企业在杭州");
        map.put("industryIntroduction", "产业在杭州");
        map.put("applicationIntroduction", "应用在杭州");
        return map;
    }

    @RequestMapping(value = "/{category}", method = RequestMethod.GET)
    public String edit(@PathVariable("category") String category, Model model) {
        AboutUs aboutUs = aboutUsService.getByCategory(category);
        if (aboutUs == null) {
            aboutUs = new AboutUs(category);
        }
        model.addAttribute("category", category);
        model.addAttribute(aboutUs);
        return "/management/aboutus/aboutUs";
    }

    @RequestMapping(value = "/{category}", method = {RequestMethod.POST, RequestMethod.PUT})
    public String save(@ModelAttribute AboutUs aboutUs, @PathVariable("category") String category, Model model) {
        model.addAttribute("category", category);
        aboutUsService.saveOrUpdateBean(aboutUs);
        return "/management/aboutus/aboutUs";
    }
}
